import redis
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from flask_session import Session
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

app = Flask(__name__)


class Config(object):
    """ 工程配置信息"""
    DEBUG = True
    # 配置mysql数据库信息
    SQLALCHEMY_DATABASE_URI = "mysql://root:mysql@127.0.0.1:3306/information"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    # redis 配置
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 6379
    # session配置
    SESSION_TYPE = "redis"  # 设置session存储到redis中
    SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)  # 设置存储的实例数据库的信息
    SESSION_USE_SIGNER = True  # session的sid 进行签名加密处理
    SESSION_PERMANENT = False  # 将session 永久存在设为False
    PERMANENT_SESSION_LIFETIME = 60 * 60 * 24  # 设置session有效期


# app加载配置
app.config.from_object(Config)
# 数据库与app联系
db = SQLAlchemy(app)
# 数据存储在Redis中
redis_store = redis.StrictRedis(host=Config.REDIS_PORT, port=Config.REDIS_PORT)
# 开启csrf保护
CSRFProtect(app)
# session配置
Session(app)
# 配置flask_srcipt及数据库迁移
manager = Manager(app)  # 实例化flask_script管理对象
Migrate(app, db)  # 将app与数据库迁移关联起来
manager.add_command("db", MigrateCommand)  # 将数据库迁移命令"db"添加到flask_script管理命令中去


@app.route("/")
def index():
    return "这是主页"


if __name__ == '__main__':
    manager.run()
